//
//  ScrollImageViewController.m
//  zhiche--delivery
//
//  Created by LeeBruce on 16/7/21.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "ScrollImageViewController.h"
#import "Header.h"
//#import "Masonry.h"
#import "Common.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import "WKProgressHUD.h"

@interface ScrollImageViewController ()<UIScrollViewDelegate>
{
    UIScrollView*Scroll;
    UILabel * Number;
    Common * com;
    NSMutableArray * pageNumberArr;
    int number;
}
@end


@implementation ScrollImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    com = [[Common alloc]init];
    
    
    [self creatScroll];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.imageArr = [[NSMutableArray alloc]init];
        self.indexNumber = 0;
    }
    return self;
}


-(void)creatScroll{
    
    pageNumberArr = [[NSMutableArray alloc]init];
    
    Scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,Main_Width,Main_height )];
    Scroll.backgroundColor = [UIColor blackColor];
    Scroll.contentSize = CGSizeMake(Main_Width*self.imageArr.count,screenHeight);
    Scroll.contentOffset = CGPointMake(self.indexNumber*Main_Width,0);
    Scroll.bounces = NO;
    Scroll.showsHorizontalScrollIndicator = NO;
    Scroll.showsVerticalScrollIndicator = NO;
    Scroll.pagingEnabled = YES;
    Scroll.delegate = self;
    
    
    
    for (int i = 0; i<self.imageArr.count; i++) {
        UIImageView * imgV =[[UIImageView alloc]initWithFrame:CGRectMake(screenWidth * i, 0, screenWidth, screenHeight)];
        imgV.contentMode = UIViewContentModeScaleAspectFit;
        
        __block UIActivityIndicatorView *activityIndicator;
        
        [imgV sd_setImageWithURL:self.imageArr[i] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            if (!activityIndicator)
            {
                activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
                activityIndicator.center = imgV.center;
                //把更新UI放到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    [imgV addSubview:activityIndicator];
                });
                
                [activityIndicator startAnimating];
            }
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            if (error) {
            
                imgV.image = [UIImage imageNamed:@"bad_picture"];
            }
            
            [activityIndicator stopAnimating];
            
        }];
        
        [Scroll addSubview:imgV];
    
     }

        UITapGestureRecognizer * single = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        //设置手指根数
        single.numberOfTouchesRequired = 1;
        //设置点击次数
        single.numberOfTapsRequired = 1;
        
        
    [Scroll addGestureRecognizer:single];
    
    
    
    self.automaticallyAdjustsScrollViewInsets =NO;
    [self.view addSubview:Scroll];
    
    
    UIButton * backBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//    backBtn.backgroundColor = [UIColor whiteColor];
//    [backBtn setBackgroundImage:[UIImage imageNamed:@"picture_back"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"picture_back"] forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(0, 20, 50, 50);
    [backBtn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
//    backBtn.frame = CGRectMake(20, 30, 35, 22);
    
    
    Number = [[UILabel alloc]initWithFrame:CGRectMake(0, screenHeight - 60, screenWidth, 20)];
    
    Number.text = [NSString stringWithFormat:@"1/%ld",self.imageArr.count];
    
    Number.textColor = [UIColor whiteColor];
    [self.view addSubview:Number];
    Number.textAlignment = NSTextAlignmentCenter;

   
}



-(void)tapAction{
    [self.navigationController popViewControllerAnimated:YES];

   
}
-(void)pressBtn{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
//    [self dismissViewControllerAnimated:YES completion:^{
//    }];
}



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    
    Number.text = [NSString stringWithFormat:@"%.0f/%ld",point.x/self.view.frame.size.width + 1,self.imageArr.count];
    
}

/*
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"呵呵";
    if (!error) {
        message = @"成功保存到相册";
        
        UIImageView * backImage = [com createUIImage:@"保存成功" andDelay:1.5f];
        [self.view addSubview:backImage];
        [backImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.centerY.mas_equalTo(self.view.mas_centerY);
        }];
        
        
        
    }else
    {
        
        message = [error description];
        UIImageView * backImage = [com createUIImage:message andDelay:1.0f];
        [self.view addSubview:backImage];
        [backImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.centerY.mas_equalTo(self.view.mas_centerY);
        }];
        
    }
    NSLog(@"message is %@",message);
}


-(UILabel*)createUIlabel:(NSString *)title andFont:(CGFloat)font andColor:(UIColor*)color {
    UILabel * label =[[UILabel alloc]init];
    label.text = title;
    label.textColor = color;
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:font];CGSize size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    // 名字的H
    CGFloat nameH = size.height;
    // 名字的W
    CGFloat nameW = size.width;
    label.frame =CGRectMake(0, 0, nameW, nameH);
    label.font = Font(font)
    return label;
}

/*
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
